{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a123f27c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a5ee5ba6",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = 'C:/Users/new/Desktop/疫情演化系统/数据/train_township.csv'\n",
    "data = pd.read_csv(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f905917d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>镇区</th>\n",
       "      <th>经停病例数</th>\n",
       "      <th>患者数量</th>\n",
       "      <th>经度</th>\n",
       "      <th>纬度</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021/12/23</td>\n",
       "      <td>东马圈镇</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>116.844191</td>\n",
       "      <td>39.444862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021/12/24</td>\n",
       "      <td>东马圈镇</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>116.844191</td>\n",
       "      <td>39.444862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021/12/25</td>\n",
       "      <td>东马圈镇</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>116.844191</td>\n",
       "      <td>39.444862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021/12/26</td>\n",
       "      <td>东马圈镇</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>116.844191</td>\n",
       "      <td>39.444862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021/12/27</td>\n",
       "      <td>东马圈镇</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>116.844191</td>\n",
       "      <td>39.444862</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    镇区  经停病例数  患者数量          经度         纬度\n",
       "0  2021/12/23  东马圈镇      0     0  116.844191  39.444862\n",
       "1  2021/12/24  东马圈镇      0     0  116.844191  39.444862\n",
       "2  2021/12/25  东马圈镇      0     0  116.844191  39.444862\n",
       "3  2021/12/26  东马圈镇      0     0  116.844191  39.444862\n",
       "4  2021/12/27  东马圈镇      0     0  116.844191  39.444862"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4c30846a",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = 'C:/Users/new/Desktop/疫情演化系统/数据/label.xlsx'\n",
    "data_label = pd.read_excel(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3efe10bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>日期</th>\n",
       "      <th>事件</th>\n",
       "      <th>经度</th>\n",
       "      <th>纬度</th>\n",
       "      <th>township</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2022-01-08</td>\n",
       "      <td>6</td>\n",
       "      <td>117.365554</td>\n",
       "      <td>39.030950</td>\n",
       "      <td>辛庄镇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2022-01-08</td>\n",
       "      <td>6</td>\n",
       "      <td>117.348388</td>\n",
       "      <td>39.032328</td>\n",
       "      <td>辛庄镇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2022-01-08</td>\n",
       "      <td>6</td>\n",
       "      <td>117.351557</td>\n",
       "      <td>39.029502</td>\n",
       "      <td>辛庄镇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2022-01-08</td>\n",
       "      <td>6</td>\n",
       "      <td>117.352308</td>\n",
       "      <td>39.033667</td>\n",
       "      <td>辛庄镇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2022-01-08</td>\n",
       "      <td>6</td>\n",
       "      <td>117.327864</td>\n",
       "      <td>39.032050</td>\n",
       "      <td>辛庄镇</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0         日期  事件          经度         纬度 township\n",
       "0           0 2022-01-08   6  117.365554  39.030950      辛庄镇\n",
       "1           1 2022-01-08   6  117.348388  39.032328      辛庄镇\n",
       "2           2 2022-01-08   6  117.351557  39.029502      辛庄镇\n",
       "3           3 2022-01-08   6  117.352308  39.033667      辛庄镇\n",
       "4           4 2022-01-08   6  117.327864  39.032050      辛庄镇"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_label.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9bc504c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_neighbor(x1, y1, x2, y2):\n",
    "    if (x1 - x2) ** 2 + (y1 - y2) ** 2 < 0.01:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "602efbb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "for index,row in data_label.iterrows():\n",
    "    date = row['日期']\n",
    "    township = row['township']\n",
    "    rows = data[(data['日期'] == date & data['township'] == township)]\n",
    "    for i in rows\n",
    "    \n",
    "    # 找出患者检测出阳性的时间\n",
    "    rows = data[(data['病例号'] == pat_id)]\n",
    "    d = rows.iloc[len(rows)-1, 1]\n",
    "    if d is not np.NaN:\n",
    "        d = datetime.strptime(d, \"%Y/%m/%d\")\n",
    "        if startdatetime < d:\n",
    "            startdatetime = d\n",
    "    rows_index = train[(train['日期'] == startdatetime) & (train['镇区'] == township)].index.tolist()\n",
    "    township\n",
    "    rows_index\n",
    "    if rows_index:\n",
    "        for r in rows_index:\n",
    "            # 这里假设一个患者有七天的影响\n",
    "            for time_lag in range(7):\n",
    "                train.loc[r+time_lag, '患者数量'] += 1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
